package cn.ante.org.service.impl;

import cn.ante.basic.exception.BusinessException;
import cn.ante.basic.utils.BaiduAuditUtils;
import cn.ante.basic.utils.MailUtil;
import cn.ante.org.domain.Employee;
import cn.ante.org.domain.Shop;
import cn.ante.org.domain.ShopAuditLog;
import cn.ante.org.mapper.EmployeeMapper;
import cn.ante.org.mapper.ShopAuditLogMapper;
import cn.ante.org.mapper.ShopMapper;
import cn.ante.org.service.IEmployeeService;
import cn.ante.org.service.IShopService;
import cn.ante.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Nai
 * @since 2022-07-01
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Override
    public void settlement(Shop shop) {

        Employee admin = shop.getAdmin();
        // 判断参数是否为空
        if (StringUtils.isEmpty(shop.getAddress()) ||
                StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(admin.getUsername()) ||
                StringUtils.isEmpty(admin.getEmail()) || StringUtils.isEmpty(admin.getPhone())
            || StringUtils.isEmpty(admin.getPassword())){
            throw new BusinessException("参数不能为空，请重新输入");
        }
        // 判断两次输入的密码是否相同
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致请重新输入");
        }

        // 开始提交代码与数据库比较判断
        // 店铺输入的名称是否一致 先根据输入的名称查找店铺数据库是否有
        Shop shop1 = shopMapper.loadByName(shop.getName());
        if (shop1 != null){
            throw new BusinessException("店铺名称已存在，请重新输入");
        }
        // 判断店铺名称是否合法
        Boolean aBoo = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoo){
            throw new BusinessException("请输入合法的店铺名称");
        }

        // 比较该员工的账户是否存在且已经绑定店铺
        Employee employee = employeeMapper.loadByName(admin.getUsername());
        if (employee != null && admin.getShopId()!= null){
            throw new BusinessException("管理员已存在且绑定了店铺，请重新提交管理员信息");
        }

        // 判断该员工未注册且未绑定店铺，先将管理员的信息保存
        if (employee == null){
            employeeService.add(admin);
        }
        // 获取管理员的id再店铺表中设置未管理员id
        shop.setAdminId(admin.getId());

        // 保存增加该店铺
        shopMapper.add(shop);

        // 获取新保存的店铺的id将其设置为管理员下的管理店铺的id
        admin.setShopId(shop.getId());

        // 修改管理员的信息为保存的管理员
        employeeService.update(admin);
    }

    @Override
    @Transactional
    public void batchAdd(List<Shop> shops) {
        shopMapper.batchAdd(shops);
    }


    /**
    * @Description: 驳回连接
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/9 21:35
    */
    @Override
        public void auditReject(ShopAuditLog shopAuditLog) {
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.add(shopAuditLog);
        Long shopId = Long.valueOf(shopAuditLog.getShopId());
        String text = "重新入驻：http://localhost:8081/#/registerAgain?id="+shopId;
        MailUtil.sendMail("594390939@qq.com",text,"驳回连接");
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(shopAuditLog.getState());
        shopMapper.update(shop);
    }

    @Override
    public void rejectAlway(ShopAuditLog shopAuditLog) {
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.add(shopAuditLog);
        MailUtil.sendMail("594390939@qq.com","入驻失败，平台拒绝入驻，请申诉","商家入驻结果");
        Shop shop = shopMapper.loadById(Long.valueOf(shopAuditLog.getShopId()));
        shop.setState(shopAuditLog.getState());
        shopMapper.update(shop);
    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog) {
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.add(shopAuditLog);
        String text = "请登录激活：http://localhost:8081/#/login";
        MailUtil.sendMail("594390939@qq.com",text,"商家入驻结果");
        Shop shop = shopMapper.loadById(Long.valueOf(shopAuditLog.getShopId()));
        shop.setState(shopAuditLog.getState());
        shopMapper.update(shop);

    }

//    @Override
//    public void image(String file) {
//        // 判断上传的图片是否合法
//        Boolean b = BaiduAuditUtils.ImgCensor(file);
//        System.out.println(b);
//        if (!b){
//            throw new BusinessException("logo图片存在违规请重新上传");
//        }
//    }


}
